import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_page/community_business/widget/community_post_widget.dart';
import 'package:xvideos/hj_page/mine_business/mine_post/mine_publish_sub_logic.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/tools_base/refresh/pull_refresh.dart';

import 'mine_publish_logic.dart';

typedef OnItemCheckCallback = void Function(VideoModel videoModel, bool isChecked);

class MinePublishSubPage extends StatelessWidget {
  final int status;
  final bool inEdit;
  final OnItemCheckCallback? onItemCheck;
  const MinePublishSubPage({super.key, this.status = 0, this.inEdit = false, this.onItemCheck});

  @override
  Widget build(BuildContext context) {
    final logic = Get.find<MinePublishLogic>();

    return GetBuilder(
      init: MinePublishSubLogic(status: status),
      tag: status.toString(),
      builder: (controller) {
        return pullYsRefresh(
          onInit: (refr) => controller.refreshController = refr,
          onRefresh: (refr) => controller.fetchPageData(),
          onLoading: (refr) => controller.fetchPageData(isRefresh: false),
          child: () {
            if (controller.isLoading) return LoadingCenterWidget();
            if (controller.dataSource.isEmpty) return CErrorWidget();
            return ListView.builder(
              itemCount: controller.dataSource.length,
              itemBuilder: (BuildContext context, int index) {
                return Padding(
                  padding: EdgeInsets.symmetric(horizontal: 16),
                  child: CommunityPostWidget(
                    videoModel: controller.dataSource[index],
                    isMyPublish: true, 
                    showCheck: inEdit,
                    isChecked: logic.checkedList.contains(controller.dataSource[index]),
                    onItemCheck: (videoModel, isChecked) {
                      if (isChecked) {
                        logic.addChecked(videoModel);
                      } else {
                        logic.removeChecked(videoModel);
                      }
                    },
                  ),
                );
              },
            );
          }(),
        );
      },
    );
  }
}
